package com.gangwangchina.android.contactlib.iterators;

import java.util.Iterator;

import com.gangwangchina.android.contactlib.model.Person;

import android.database.Cursor;
import android.util.Log;



public class PersonIterator implements Iterator<Person>{

	private Cursor c;
	
	/**
	 * Creates a PersonIterator over a Cursor
	 * @param c
	 */
	public PersonIterator(Cursor c){
		this.c = c;
	}
	
	
	public boolean hasNext() {
		return !(c.isLast()||c.isAfterLast());
	}

	public Person next() {
		c.moveToNext();
		return currentPerson();
	}
	
	/**
	 * Returns the previous object and moves back the iterator
	 * @return the previous object
	 */
	public Person previous(){
		c.moveToPrevious();
		return currentPerson();
	}

	public void remove() {
		Log.e("PersonItertaor", "Remove doesn't have any effect on a PersonIterator");
	}
	
	/**
	 * This method closes the Cursor used to retrieves data from the database
	 * Therefore, it should be called whenever the PersonIterator is not needed anymore
	 */
	public void close(){
		c.close();
	}
	
	private Person currentPerson(){
		Person p = Person.recoverPerson(c.getLong(0), c.getString(1));
		return p;
	}
	

}
